package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.util.OSSClientUtil;
import net.zigin.nhi.could.waste.util.OSSQiniuUtil;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * SysDiction控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysDictionController
 * @date 2021-9-20 22:06:06
 */
@Api(tags = "文件上传管理")
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private OSSClientUtil ossClientUtil;
    @Autowired
    private OSSQiniuUtil ossQiniuUtil;

    @PostMapping("/uploadFile")
    @ApiOperation("上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "版本id", required = true, dataType = "MultipartFile")
    })
    public JsonForResult uploadFile(@RequestParam("file") MultipartFile file) {
        String path = ossClientUtil.updateHead(file);
        return JsonForResult.builder().data(path).success(true).build();
    }

    @PostMapping("/uploadFiles")
    @ApiOperation("多上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "版本id", required = true, allowMultiple = true, dataType = "MultipartFile")
    })
    public JsonForResult uploadFile(@RequestParam("files") List<MultipartFile> files) {
        StringBuffer allPath = new StringBuffer();
        files.forEach(file -> {
            String path = ossClientUtil.updateHead(file);
            allPath.append(path).append(",");
        });
        return JsonForResult.builder().data(allPath.toString()).success(true).build();
    }

    @LoginWhitePathAnnotation
    @PostMapping("/uploadApp")
    @ApiOperation("上传文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "版本id", required = true, dataType = "MultipartFile")
    })
    public JsonForResult uploadApp(@RequestParam("file") MultipartFile file) {
        String path = ossQiniuUtil.uploadFile(file);
        return JsonForResult.builder().data(path).success(true).build();
    }
}
